<?
$lUseAjax=1;
$pagename=LA_SEARCH;
include_once("header_inc.php");

$searchpage=1;
$catid_search = getParamInt("catid_search","");
$lDoNotCreateTemplate=1;
$tplList=new TplLoad;
if (getParam("reset",""))
	redirect("search.php");

$lSearchWordTpl=getParam("searchword","");

$tplList->assign("set_region","$set_region");
$tplList->assign("searchword",$lSearchWordTpl);
$tplList->assign("catlist",getCategoriesDD(getParamInt("catid_search",""),""));
$tplList->assign("set_currency_text","$set_currency_text");
$lExtraAdd="";
		
// Check if we have a category id to search in
if($catid_search>0)
{
	if (!$_REQUEST["catid_search"]=="%%")
	{
		$result_1 = q ("select cat_tpl from $cat_tbl where cat_id = ".round($_REQUEST["catid_search"]));
		$rad=mysql_fetch_array($result_1);
		$cattpl_cat = $rad["cat_tpl"];
	}
}
		
$result_1 = q ("select cat_tpl from $cat_tbl where cat_id = '$catid_search'");
$row_cat = mysql_fetch_array($result_1);
$cat_tpl = $row_cat["cat_tpl"];

/* Extra fields handling START */
$lFieldArray=getExtraFiltered($cat_tpl,"","",$set_language);


if (is_array($lFieldArray))
{
	foreach ($lFieldArray as $lKey => $lValue)
	{
		if ($lValue["q_question"])
			$lFieldArray[$lKey]["q_question"]=(constant($lValue["q_question"]));
		if ($lValue["q_catdescr"])
			$lFieldArray[$lKey]["q_catdescr"]=(constant($lValue["q_catdescr"]));
	}
}

$lJoinedInputArray=array_merge($_POST,$_GET);

/* Special code to fix so that intervals (from-to) can be performed */
foreach ($lJoinedInputArray as $key => $value) {
	//echo "$key - $value<br>";
	if (ereg("from",$key))
	{
		$key=ereg_replace("e_","",$key);
		$key=ereg_replace("_from","",$key);
		$lFieldArray["$key"]["q_value_from"]=$value;
	}
	elseif (ereg("to",$key))
	{
		$key=ereg_replace("e_","",$key);
		$key=ereg_replace("_to","",$key);
		$lFieldArray["$key"]["q_value_to"]=$value;
	
	}
}
$tplList->assign("extra_fields_array",$lFieldArray); // Add question to template

if (is_array($lFieldArray))
{
	$lReturnResultArray=ExtraFieldsInputOperations($lFieldArray);
	$lInsertArray=$lReturnResultArray[0];	// Contains data to be inserted to extra
	$errorMsg=$lReturnResultArray[1]; 		// Contains error messages
}
/* Extra fields handling FINISH */


if (count($lLevelArray)==3)
{
	$lReg3Selected=$lLevelArray[1]["reg_id"];
	$lReg3Father=$lLevelArray[1]["reg_father"] . "_";
	
	$lReg2Selected=$lLevelArray[2]["reg_id"];
	$lReg2Father=$lLevelArray[2]["reg_father"];
	
	$lReg1Selected=$lLevelArray[3]["reg_id"];
	
	
}
else if (count($lLevelArray)==2) 
{
	$lReg2Selected=$lLevelArray[1]["reg_id"];
	$lReg2Father=$lLevelArray[1]["reg_father"];

}

$lReg2Father=getParam("reg_1",$lReg2Father);
$lReg2Selected=getParam("reg_2",$lReg2Selected);

if ($lReg2Father && $lReg2Selected!="")
	$lReg2String=$lReg2Father . "|" . $lReg2Selected;
else 
	$lReg2String=$lReg2Father;

$lReg3Father=getParam("reg_2",$lReg3Father);
$lReg3Selected=getParam("reg_3",$lReg3Selected);


if ($lReg3Father && $lReg3Selected!="")
	$lReg3String=$lReg3Father . "|" . $lReg3Selected;
else 
	$lReg3String=$lReg3Father;

$tplList->assign("reg_2_value",$lReg2String);
$tplList->assign("reg_3_value",$lReg3String);


$tplList->assign("options",getRegionTree(0,0,$lSelected));
$lRegOptions="<option value=''>".LA_REGION_CHOOSE_FIRST."</option>";
$lRegOptions.=getRegionsForOneLevel(0,getParam("reg_1",$lReg1Selected),"");
$tplList->assign("reg_1_options",$lRegOptions);

if (getParam("do_search","") || getParam("searchword","")!="")
	require("links.php");
else
{ 
	$tplList->assign("hide",1);
	//$tplList->display("search_links.tpl");
	//JMR: Cambio de template para busquedas
	//logMsg("JMR: template que estamos mostrando","latest_frontpage.tpl");
	$tplList->display("latest_frontpage.tpl");
}
include_once("footer_inc.php");
?>

